package cn.kkdlk.generate.dxf.model;

import cn.kkdlk.generate.dxf.Vector3;
import cn.kkdlk.generate.dxf.model.base.BaseDxfEntity;
import cn.kkdlk.generate.dxf.util.DxfLineBuilder;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

/**
 * 多段线图元
 *
 * @author YTZJJ
 */
@AllArgsConstructor
@Getter
@Setter
public class DxfLwPolyLine extends BaseDxfEntity {

    /**
     * 各个点的集合
     */
    private List<Vector3> points;
    /**
     * 多线段是否闭合
     */
    private boolean isClose = false;


    public DxfLwPolyLine() {
        points = new ArrayList<>();
    }

    /**
     * 向多线段中添加一个坐标点
     *
     * @param p 坐标点
     */
    public void addPoint(Vector3 p) {
        points.add(p);
    }

    public void addPoints(List<Vector3> p) {
        points.addAll(p);
    }

    /***
     * 从多线段中移除一个坐标点
     *
     * @param p 坐标点
     */
    public void removePoint(Vector3 p) {
        points.remove(p);
    }

    /**
     * 多线段图元中内容是否为空
     *
     * @return 若图元空不包含任何坐标点返回true
     */
    public boolean isEmpty() {
        return points.isEmpty();
    }

    @Override
    protected String getChildDxfStr() {
        DxfLineBuilder lineBuilder = DxfLineBuilder.build();
        lineBuilder.append(90, points.size());
        lineBuilder.append(70, isClose ? 1 : 0);
        lineBuilder.append(43, 0.0);
        for (Vector3 point : points) {
            lineBuilder
                    .append(10, point.getX())
                    .append(20, point.getY())
                    .append(30, point.getZ());
        }
        return lineBuilder.toString();
    }

    @Override
    public String getEntityName() {
        return "LWPOLYLINE";
    }

    @Override
    public String getEntityClassName() {
        return "AcDbPolyline";
    }
}
